package com.yuerlive.rainbow.module.main.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseFragment;
import com.yuerlive.rainbow.entity.LiveInfo;
import com.yuerlive.rainbow.entity.ObjectWrapper;
import com.yuerlive.rainbow.entity.VersionInfo;
import com.yuerlive.rainbow.module.live.LiveRoomActivity;
import com.yuerlive.rainbow.module.main.adapter.MainHotAdapter;
import com.yuerlive.rainbow.module.main.presenter.HomeContract;
import com.yuerlive.rainbow.module.main.presenter.HomePresenter;
import com.yuerlive.rainbow.module.main.presenter.MainContract;
import com.yuerlive.rainbow.module.main.presenter.MainPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by xiaoyi on 2017/6/28.
 */

public class FragmentTest extends BaseFragment<HomeContract.Presenter> implements HomeContract.View {

    @BindView(R.id.recycleView)
    RecyclerView recyclerView;
    MainHotAdapter adapter;
    List<ObjectWrapper> mDatas = new ArrayList<>();
    @Override
    public void setPresenter(HomeContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }



    @Override
    protected int getLayoutId() {
        return R.layout.fragment_test;
    }

    @Override
    public void initViews(View root) {
        presenter = new HomePresenter(this);
        presenter.getFollowed(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new MainHotAdapter(mActivity, mDatas);
        adapter.setOnItemClickListener((v, p) -> {
            //临时解决mDatas数据为空的时候点击item
            if (mDatas.size() > 0) {
                Intent i = new Intent();
                i.setClass(mActivity, LiveRoomActivity.class);
                LiveInfo info = (LiveInfo) mDatas.get(p).object;
                i.putExtra("liveId", info.getLiveId());
                i.putExtra("chatroomId", info.getChatRoomId());
                i.putExtra("liveCover", info.getLiveIcon());
                jumpToActivity(i);
            }
        });
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void showFullData(List<ObjectWrapper> results) {
        mDatas.clear();
        mDatas.add(new ObjectWrapper(MainHotAdapter.TYPE_HEADER, null));
        mDatas.addAll(results);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showLiveData(List<ObjectWrapper> results, int page) {
        mDatas.clear();
        mDatas.add(new ObjectWrapper(MainHotAdapter.TYPE_HEADER, null));
        mDatas.addAll(results);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showMoreData(List<ObjectWrapper> results) {

    }
}
